package com.cws4cloud.cws.mall.cms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cws4cloud.cws.common.core.constant.CommonConstants;
import com.cws4cloud.cws.common.core.util.TreeUtil;
import com.cws4cloud.cws.mall.cms.dto.ClasslistTree;
import com.cws4cloud.cws.mall.cms.entity.Classlist;
import com.cws4cloud.cws.mall.cms.mapper.ClasslistMapper;
import com.cws4cloud.cws.mall.cms.service.ClasslistService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 栏目管理
 *
 * @author zhuljf
 * @date 2021-04-20 17:06:53
 */
@Service
@RequiredArgsConstructor
public class ClasslistServiceImpl extends ServiceImpl<ClasslistMapper, Classlist> implements ClasslistService {

	private final ClasslistMapper classlistMapper;

	@Override
	public List<ClasslistTree> listClasslistTrees(boolean lazy, Integer parentId, String name, Integer siteId) {
		if (lazy){
			Integer parent = parentId == null ? CommonConstants.MENU_TREE_ROOT_ID : parentId;
			return  TreeUtil.build(classlistMapper.listClasslistTree(parent,siteId),parent);
		} else {
			return buildTree(classlistMapper.selectList(Wrappers.<Classlist>lambdaQuery().eq(Classlist::getSiteId,siteId).orderByAsc(Classlist::getSequence)),-1);
		}
	}

	/**
	 * 通过sysMenu创建树形节点
	 * @param classlists
	 * @param root
	 * @return
	 */
	public List<ClasslistTree> buildTree(List<Classlist> classlists, int root) {
		List<ClasslistTree> trees = new ArrayList<>();
		ClasslistTree node;
		for (Classlist classlist : classlists) {
			node = new ClasslistTree();
			BeanUtil.copyProperties(classlist, node, true);
			node.setHasChildren(false);
			trees.add(node);
		}
		return TreeUtil.build(trees, root);
	}

}
